From 8ab2e31b5807696e2e8385980ed29dd7cc93f514 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 26 Mar 2020 13:51:59 -0400 Subject: [PATCH] widget-factory: Add theme switch Add a submenu to test the builtin themes. This is mainly for the benefit of the artists working on those themes. --- demos/widget-factory/widget-factory.c | 61 +++++++++++++++++++++++++- demos/widget-factory/widget-factory.ui | 34 ++++++++++++-- 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c index 2445af7c27..9dd12e62c9 100644 --- a/demos/widget-factory/widget-factory.c +++ b/demos/widget-factory/widget-factory.c @@ -26,7 +26,7 @@ #include static void -change_theme_state (GSimpleAction *action, +change_dark_state (GSimpleAction *action, GVariant *state, gpointer user_data) { @@ -40,6 +40,57 @@ change_theme_state (GSimpleAction *action, g_simple_action_set_state (action, state); } +static char *current_theme; +static gboolean current_dark; + +static void +change_theme_state (GSimpleAction *action, + GVariant *state, + gpointer user_data) +{ + GtkSettings *settings = gtk_settings_get_default (); + const char *s; + const char *theme; + gboolean prefer_dark = FALSE; + + s = g_variant_get_string (state, NULL); + + if (strcmp (s, "adwaita") == 0) + { + theme = "Adwaita"; + prefer_dark = FALSE; + } + else if (strcmp (s, "adwaita-dark") == 0) + { + theme = "Adwaita"; + prefer_dark = TRUE; + } + else if (strcmp (s, "highcontrast") == 0) + { + theme = "HighContrast"; + prefer_dark = FALSE; + } + else if (strcmp (s, "highcontrast-inverse") == 0) + { + theme = "HighContrastInverse"; + prefer_dark = FALSE; + } + else if (strcmp (s, "current") == 0) + { + theme = current_theme; + prefer_dark = current_dark; + } + else + return; + + g_object_set (G_OBJECT (settings), + "gtk-theme-name", theme, + "gtk-application-prefer-dark-theme", prefer_dark, + NULL); + + g_simple_action_set_state (action, state); +} + static GtkWidget *page_stack; static void @@ -1671,7 +1722,8 @@ activate (GApplication *app) GtkCssProvider *provider; GMenuModel *model; static GActionEntry win_entries[] = { - { "dark", NULL, NULL, "false", change_theme_state }, + { "dark", NULL, NULL, "false", change_dark_state }, + { "theme", NULL, "s", "'current'", change_theme_state }, { "transition", NULL, NULL, "false", change_transition_state }, { "search", activate_search, NULL, NULL, NULL }, { "delete", activate_delete, NULL, NULL, NULL }, @@ -1703,6 +1755,11 @@ activate (GApplication *app) GPermission *permission; GAction *action; + g_object_get (gtk_settings_get_default (), + "gtk-theme-name", ¤t_theme, + "gtk-application-prefer-dark-theme", ¤t_dark, + NULL); + g_type_ensure (my_text_view_get_type ()); provider = gtk_css_provider_new (); diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui index fd6be0c188..ecca3b4ff1 100644 --- a/demos/widget-factory/widget-factory.ui +++ b/demos/widget-factory/widget-factory.ui @@ -6,10 +6,36 @@ Get Busy win.busy - - Dark Theme - win.dark - + + Theme +
+ + Current theme + win.theme + current + + + Adwaita + win.theme + adwaita + + + Adwaita dark + win.theme + adwaita-dark + + + High contrast + win.theme + highcontrast + + + High contrast inverse + win.theme + highcontrast-inverse + +
+
Slide Pages win.transition -- 2.30.2